package jdsk.iria;

import java.util.ArrayList;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

public class TextViewWindow extends Activity implements
		OnGesturePerformedListener {

	WebView imageViewer;
	GestureLibrary gestLib;
	private int countOfGestures;
	private static String studentName;
	private static String teacherName;
	private static String fileName;
	private int numOfWords;

	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_text_view);

		imageViewer = (WebView) findViewById(R.id.webView1);
		countOfGestures = 0;

		// This is a working set for a webview (WV)
		imageViewer.getSettings().setLoadWithOverviewMode(true);
		imageViewer.getSettings().setUseWideViewPort(true);
		// imageViewer.getSettings().setDisplayZoomControls(true);
		imageViewer.getSettings().setBuiltInZoomControls(true);
		// imageViewer.getSettings().setEnableSmoothTransition(true);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {

			studentName = extras.getString("studentName");
			teacherName = extras.getString("teacherName");
			fileName = extras.getString("fileName");
			numOfWords = extras.getInt("numOfWords");
			String fileURL = extras.getString("imageFile");
			fileURL = "File://" + fileURL;
			imageViewer.loadUrl(fileURL);

		}

		GestureOverlayView myGestureOverlay = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
		myGestureOverlay.addOnGesturePerformedListener(this);
		gestLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
		gestLib.load();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_text_view, menu);
		return true;
	}

	@Override
	public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
		for (String gestureStrings : gestLib.getGestureEntries()) {

		}
		ArrayList<Prediction> listOfPred = gestLib.recognize(gesture);
		for (Prediction prediction : listOfPred) {
			if (prediction.score >= 7.0) {
				countOfGestures += 1;

				Toast.makeText(this, "Wrong Words: [" + countOfGestures + "]",
						Toast.LENGTH_LONG).show();
				break;

			} else {

				// Toast.makeText(this,
				// "Boo FaMiLy!!!!!",Toast.LENGTH_LONG).show();

			}

		}

	}

	public void textResetButtonClick(View view) {
		countOfGestures = 0;
		Toast.makeText(this, "Count Reset [" + countOfGestures + "]",
				Toast.LENGTH_LONG).show();
	}

	public void textFinishButtonClick(View view) {

		Bundle dataBundle = new Bundle();
		dataBundle.putString("fileName", fileName);
		dataBundle.putString("studentName", studentName);
		dataBundle.putString("teacherName", teacherName);
		dataBundle.putInt("countOfGestures", countOfGestures);
		dataBundle.putInt("numOfWords", numOfWords);

		Intent myIntent = new Intent(this, AnalysisActivity.class);
		myIntent.putExtras(dataBundle);
		this.startActivity(myIntent);

	}

	/**
	 * handles the start button being clicked
	 * @param view
	 */
	public void textStartClick(View view) {

		Toast.makeText(this,
				"  The Timer Feature Has Not Been Implemented Yet!!",
				Toast.LENGTH_LONG).show();

	}

}
